package com.tianchi.lexiang.background;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.tianchi.lexiang.R;
import com.tianchi.lexiang.background.util.OsdSettingUtil;

public class AddOsd extends Activity implements OnClickListener {

	private static final String TAG = "AddOsd";
	private OsdSettingUtil instance = OsdSettingUtil.getInstance();

	private EditText osdTxt;

	private Button saveBtn;
	private Button backBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_osd);
		initView();
	}

	private void initView() {
		osdTxt = (EditText) findViewById(R.id.osd_txt);
		saveBtn = (Button) findViewById(R.id.save_btn);
		saveBtn.setOnClickListener(this);
		backBtn = (Button) findViewById(R.id.back_btn);
		backBtn.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.save_btn:
			String osdStr = osdTxt.getText().toString();
			Log.d(TAG, "[onClick]...osdStr===>" + osdStr);
			if (osdStr != null && !"".equals(osdStr)) {
				int numbers = instance.getSodSubTitlesNumber();
				Log.d(TAG,"[onClick]...numbers==>" + numbers);
				if (numbers >= 10) {
					Log.d(TAG, "[onClick]...delete");
					instance.deleteOldOsdSubtitle();
				}
				Log.d(TAG, "[onClick]...insert");
				boolean isSuccess = instance.insertOsdSubTitle(osdStr);
				Log.d(TAG, "[onClick]........isSuccess====>" + isSuccess);
				if (isSuccess) {
					OsdSetting.mAdapter.startQueryAlreadyPonit();
					successDialog();
				}
			} else {
				warDialog();
			}
			break;
		case R.id.back_btn:
			AddOsd.this.finish();
			break;

		default:
			break;
		}

	}

	protected void warDialog() {
		AlertDialog.Builder builder = new Builder(AddOsd.this);
		builder.setMessage(getString(R.string.please_input_osd_content));
		builder.setIcon(android.R.drawable.ic_menu_info_details);
		builder.setTitle(getString(R.string.warn));

		builder.setPositiveButton(getString(R.string.confirm), new Dialog.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.create().show();
	}

	protected void successDialog() {
		AlertDialog.Builder builder = new Builder(AddOsd.this);
		builder.setMessage(getString(R.string.osd_add_success));
		builder.setIcon(android.R.drawable.ic_menu_info_details);
		builder.setTitle(getString(R.string.tip));

		builder.setPositiveButton(getString(R.string.yes), new Dialog.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				AddOsd.this.finish();
			}
		});
		builder.setNegativeButton(getString(R.string.no), new Dialog.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				osdTxt.setText("");
				dialog.dismiss();
			}

		});
		builder.create().show();
	}

}
